//
//
//
//
//
//
// NetAddr.C

#include "NetAddr.h"
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/socketvar.h>

#include "StrStream.h"

NetAddr::NetAddr() : 
	_afamily(_AF_NONE), _port(0), _ip(0), _valide(false){};

NetAddr::NetAddr(U32 afamily, S32 port, const std::string& ip)
{
	_afamily = afamily;
	_port = htons(port);
	_ip = inet_addr(ip.c_str());

	if(_ip == -1)
		_valide = false;
	_valide = true;
}

NetAddr::NetAddr(U32 afamily, S32 port, S32 ip)
{
	_afamily = afamily;
	_port = htons(port);
	_ip = ip;

	_valide = true;
}

NetAddr::NetAddr(const NetAddr& other) : 
	_afamily(other._afamily),
	_port(other._port),
	_ip(other._ip),
	_valide(other._valide){};


NetAddr::~NetAddr()
{};

NetAddr& NetAddr::operator=(const NetAddr& other)
{
	_afamily = other._afamily;
	_port = other._port;
	_ip = other._ip;
	_valide = other._valide;

	return *this;
}

bool NetAddr::Valide() const
{
	return _valide;
}

std::string NetAddr::AsString() const
{
	Util::StrStream str;
	str << "AFamily : " << _afamily << ", "
		<< "Port : " << ntohs(_port) << ", "
		<< "IP : " << _ip;
	return str.AsString();
}
